package com.itheima.app.manager;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.RandomUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.autoconfig.im.HuanXinTemplate;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.RecommendUser;
import com.itheima.service.mongo.FriendService;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.RecommendUserService;
import com.itheima.service.mongo.UserLikeService;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.RecommendUserVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class UserLikeManager {
    @DubboReference
    private UserLikeService userLikeService;
    @DubboReference
    private FriendService friendService;

    @Autowired
    private HuanXinTemplate huanXinTemplate;
    @DubboReference
    private UserInfoService userInfoService;

    @DubboReference
    private RecommendUserService recommendUserService;

    /**
     * 查询推荐好友
     */
    public PageBeanVo findRecommendUser(Integer pageNumber, Integer pageSize) {
        //1.获得用户的id
        Long loginUserId = UserHolder.get().getId();
        //2.调用service进行查询 返回PageBeanVo
        PageBeanVo pageBeanVo = recommendUserService.findByPage(loginUserId,pageNumber , pageSize  );
        //3.获得集合对象
        List<RecommendUser> items = (List<RecommendUser>) pageBeanVo.getItems();
        //4.判断集合是否为空 如果为空 构建假数据
        if(CollectionUtil.isEmpty(items)){
            items = new ArrayList<>();
            for(int i = 2 ; i <= 12 ; i ++){//推荐十个用户
                RecommendUser recommendUser = new RecommendUser();
                recommendUser.setToUserId( loginUserId );
                recommendUser.setUserId( Long.valueOf(i) ); //推荐默认用户
                recommendUser.setScore(RandomUtil.randomDouble(60, 90)); //推荐的默认缘分值
                items.add(recommendUser);
            }
        }
        List<RecommendUserVo> voList = new ArrayList<>();
        //4.判断集合是否为空 如果不为空 遍历数据
        for (RecommendUser recommendUser : items) {
            //5.根据遍历的推荐用户查询 用户的基础信息
            UserInfo userInfo = userInfoService.findById(recommendUser.getUserId());
            //6.根据基础信息构建 RecommendUserVo对象
            RecommendUserVo vo = new RecommendUserVo();
            vo.setUserInfo(userInfo);
            vo.setFateValue( recommendUser.getScore().longValue() );
            voList.add(vo);
        }

        //7.返回
        pageBeanVo.setItems(voList);
        return pageBeanVo;
    }

    public Map love(Long likeUserId) {
        //1.获得当前用户id
        Long loginUserId = UserHolder.get().getId();
        //2.保存用户的喜欢
        userLikeService.saveUserLike(loginUserId , likeUserId);

        //3.调用方法判断用户是否互相喜欢
        boolean flag = userLikeService.isUserLike(loginUserId , likeUserId );

        if(flag){
            //4.true 相互喜欢
            //4.1 加friend好友关系
            friendService.addContact(loginUserId , likeUserId);
            //4.2 加环信的好友关系
            huanXinTemplate.addContacts(String.valueOf(loginUserId) , String.valueOf(likeUserId));
        }

        return null;
    }

    public void unlove(Long likeUserId) {
        //1. 获取登录用户id
        Long userId = UserHolder.get().getId();
        //2. 调用friendService删除好友
        userLikeService.delete(userId, likeUserId);
        //3. 调用环信删除好友
        huanXinTemplate.deleteContacts(String.valueOf(userId), String.valueOf(likeUserId));
        //4. 调用userLikeService删除我对它的喜欢
        userLikeService.delete(userId, likeUserId);

    }

}


